package com.it.myiotest;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test1 {
    public static void main(String[] args) throws IOException {
        /*
        制造假数据：
             获取姓氏：https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0
             获取男生名字：http://www.haoming8.cn/baobao/10881.html
             获取女生名字：http://www.haoming8.cn/baobao/7641.html
         */
        //1.定义变量，记录网址
        String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
        String boyNameNet = "http://www.haoming8.cn/baobao/10881.html";
        String girlNameNet = "http://www.haoming8.cn/baobao/7641.html";

        //2.爬取数据，把网址上的所有的数据拼接为一个字符串
        String familyNameStr = webCrawler(familyNameNet);
        String boyNameStr = webCrawler(boyNameNet);
        String girlNameStr = webCrawler(girlNameNet);

        //System.out.println(boyName);

        //3.通过正则表达式，把其中符合要求的数据获取出来
        ArrayList<String> familyNameTampList = getData(familyNameStr,"([\\u4E00-\\u9FA5]{4})(，|。)",1);
        ArrayList<String> boyNameTampList = getData(boyNameStr,"([\\u4E00-\\u9FA5]{2})(、|。)",1);
        ArrayList<String> girlNameTampList = getData(girlNameStr,"(.. ){4}..",0);
        //System.out.println(boyNameTampList);

        //4.数据的处理
        //姓氏拆开
        ArrayList<String> familyNameList = new ArrayList<>();
        for(String str : familyNameTampList){
            for(int i = 0; i<str.length(); i++){
                char ch = str.charAt(i);
                //System.out.println(ch);
                familyNameList.add(ch+"");
            }
        }
        //男生的名字
        //去除其中的重复元素
        ArrayList<String> boyNameList = new ArrayList<>();
        for(String str : boyNameTampList){
            if(!boyNameTampList.contains(str)){
                boyNameList.add(str);
            }
        }
        //女孩的名字
        //把里面的每一个元素用空格进行切割，得到每一个女生的名字
        ArrayList<String> girlNameList = new ArrayList<>();
        for(String str : girlNameTampList){
            String[] arr = str.split(" ");//分割字符串
            for(int i = 0; i<arr.length; i++){
                girlNameList.add(arr[i]);
            }
        }
        //System.out.println(boyNameList);
        //5.拼接姓氏+名字
        //getInfos(familyNameList,boyNameList,girlNameList,70,50);

    }

    /*public static ArrayList<String> getInfos(ArrayList<String> familyNameList,ArrayList<String> boyNameList,ArrayList<String> girlNameList,int boycount,int girlcount){
        //生成男生不重复的名字
        HashSet<String> boyhs = new HashSet<String>();
        while(true){
            if(boyhs.size()==boycount){
                break;
            }
            //随机
            Collections.shuffle(familyNameList);
            Collections.shuffle(boyNameList);
            boyhs.add(familyNameList.get(0)+boyNameList.get(0));
        }
        //生成女生不重复的姓名
        HashSet<String> girlhs = new HashSet<String>();
        while(true){
            if(girlhs.size()==girlcount){
                break;
            }
            //随机
            Collections.shuffle(familyNameList);
            Collections.shuffle(girlNameList);
            girlhs.add(familyNameList.get(0)+girlNameList.get(0));
        }
        System.out.println(boyhs);
        return null;
    }*/
    private static ArrayList<String> getData(String str, String regex,int index) {
        //1.创建集合存放数据
        ArrayList<String> list = new ArrayList<>();//2.按照正则表达式的规则，去获取数据
        Pattern pattern = Pattern.compile(regex);
        //按照pattern的规则，到str当中获取数据
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()){
             list.add(matcher.group(index));
        }
        return list;

    }

    public static String webCrawler(String net) throws IOException {
        //1.定义StringBuilder拼接爬取到的数据
        StringBuilder sb = new StringBuilder();
        //2.创建一个URL对象
        URL url = new URL(net);
        //3.连接上这个网络
        //细节：保证网络是通畅的，而且这个网址是可以连接上的
        URLConnection connection = url.openConnection();
        //4.读取数据
        //字节流转为字符流
        InputStreamReader isr = new InputStreamReader(connection.getInputStream());
        //利用循环读取数据
        int ch;
        while((ch = isr.read()) != -1) {
            sb.append((char)ch);
        }
        //释放资源
        isr.close();
        //返回字符串
        return sb.toString();
    }
}
